﻿(function () {
    /* Copyright 2010 Imaginary Development. All rights reserved http://imaginarydevelopment.com */
    /* Gen 3 script */
    var scriptName = "RecorderGen3";
    var jQueryUri = "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js";
    var destinationUrl = 'http://imaginarydevelopment.com/Sfc/Recorder/Save';
    function DoOnceJquerified() {

        if (typeof (jQuery) == 'undefined') {
            return alert('jQuery not defined');
        }

        if (typeof ($jq) == 'undefined') {
            if (typeof ($) != 'undefined') {
                $jq = $;
            } else {
                return alert('$jq->jquerify not defined');
            }
        }

        if ($jq('html#facebook').length > 0) {
            alert("Script doesn't work in facebook");
            throw ("Facebook detected");
        }

        if ($jq('div#nav_bar a.selected:contains(Galaxy)').length != 1) {
            alert('This script is for your starfleet commander galaxy view');
            throw "Not on message screen";
        }

        var token = $jq('script[token]').attr("token");
        if (typeof (token) != 'string' || token.length <= 0) {
            alert('Token not found aborting');
            throw "No user token found for recording";

        }


        //window.alert('bookmarklet started');
        String.prototype.trim = function () {
            /* /^\s+|\s+$/g,'' */
            return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '').replace(/[\r\n\s]+|\s\s+|\s*\&nbsp\s*/g, ' ');
        };
        function ProcessPlanet(planets, index, tr) {
            planets.push('planet' + index);
        }

        function ProcessGalaxy() {
            var universe = $jq('title').text();
            var locationArea = $jq('div.current_solar_system');
            var matches = $jq(locationArea).text().match('([0-9]+):([0-9]+)');
            var galaxy = matches[1];

            //var galaxy = $jq('input#galaxy').val();
            var system = matches[2];
            //var system = $jq('input#solar_system').val();
            var planetInfo = new Array();

            $jq('table#planets.planets tr.planet').each(function (index, tr) {
                var slot = $jq('td.slot', tr).text();
                var player = $jq('td.player span:first', tr).text().trim();
                var symbols = $jq('td.player span:first span.symbols', tr);
                var status = '';
                if (symbols)
                    status = symbols.text();

                planetInfo[index] = galaxy.trim() + ':' + system.trim() + ':' + slot + "," + player + ',' + status +
             ',' + $jq('td.alliance span a', tr).text().trim();
            });
            /*alert('data will be:'+planetInfo);*/
            var postData = { 'Planets': planetInfo, 'Token': token, 'Universe': universe };
            destination.data = JSON.stringify(postData);
            $jq.ajax(destination);
        }

        function AjaxSuccess(data, textStatus, xmlHttpRequest) {

            if (typeof (data) == 'undefined') {
                return alert('Data is undefined');
            }
            var summary = data | 'ajax success, data may not have saved';
            $jq('div.title div.description').text(summary + ' snapshots inserted or updated');
            if (data == null) {
                alert(summary);
            }

        }
        function AjaxError(xmlHttpRequest, textStatus, errorThrown) {
            $jq('div.title div.description').text('ajax failure:' + textStatus+' - '+errorThrown);
        }
        var destination = { url: destinationUrl, type: 'POST', success: AjaxSuccess, error: AjaxError,
            contentType: 'text/plain'
        };

        if ($jq('body').length <= 0) {
            return alert('Could not query body length');
        }


        ProcessGalaxy();

        /* debris calculator */
        $jq('td.debris table.debris_table tr td.resources').each(function (index, td) {
            var text = $jq(td).text();
            var match = text.match('([0-9,]+)');
            if (match.length == 2) {
                var total = parseInt(match[0].replace(/,/g, ''), 10) + parseInt(match[1].replace(/,/g, ''), 10);
                $jq(td).html($jq(td).html() + '<br />' + (total / 20000));
            }
        });
        /* end debris calculator */

        /* End AjaxBookmarklet.js */
    }; //End Do Once Jquerified


    var el = document.createElement('div'), b = document.getElementsByTagName('body')[0]; otherlib = false, msg = ''; el.style.position = 'fixed'; el.style.height = '32px'; el.style.width = '220px'; el.style.marginLeft = '-110px'; el.style.top = '0'; el.style.left = '50%'; el.style.padding = '5px 10px 5px 10px'; el.style.zIndex = 1001; el.style.fontSize = '12px'; el.style.color = '#222'; el.style.backgroundColor = '#f99'; if (typeof jQuery != 'undefined') {
        msg = scriptName + ' This page already using jQuery v' + jQuery.fn.jquery;
        DoOnceJquerified();
        return showMsg();
    } else if (typeof $ == 'function') { otherlib = true; } function getScript(url, success) { var script = document.createElement('script'); script.src = url; var head = document.getElementsByTagName('head')[0], done = false; script.onload = script.onreadystatechange = function () { if (!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) { done = true; success(); } }; head.appendChild(script); } getScript(jQueryUri, function () {
        if (typeof jQuery == 'undefined') { msg = 'Sorry, but jQuery wasn\'t able to load'; } else {

            msg = scriptName + ' This page is now jQuerified with v' + jQuery.fn.jquery; if (otherlib) { msg += ' and noConflict(). Use $jq(), not $().'; }
            /* Begin AjaxBookmarklet.js */
            DoOnceJquerified();
            /* End AjaxBookmarklet.js */
        } return showMsg();
    }); function showMsg() { el.innerHTML = msg; b.appendChild(el); window.setTimeout(function () { if (typeof jQuery == 'undefined') { b.removeChild(el); } else { jQuery(el).fadeOut('slow', function () { jQuery(this).remove(); }); if (otherlib) { $jq = jQuery.noConflict(); } } }, 2500); }
})();

   
       